//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.topvision.myviewtest.well.barcode;

import android.content.Context;
import android.hardware.Camera.Size;

import com.honeywell.barcode.*;
import com.honeywell.camera.CameraManager;
import com.honeywell.misc.DeviceInfo;
import com.honeywell.misc.HSMLog;

public class DecodeManager {
    private static final String JNI_LIB = "jniload";
    private static DecodeManager instance = null;
    private Context context;
    private boolean decoderEnabled = true;
    private int actRes = 0;

    private native int LoadDecoder(Context var1);

    private native int RemoteActivation(Context var1, byte[] var2, byte[] var3);

    private native int LocalFileActivation(Context var1, byte[] var2, byte[] var3);

    private native int LocalServerActivation(Context var1, byte[] var2, byte[] var3, byte[] var4, byte[] var5);

    private native int Deactivate(Context var1, byte[] var2);

    private native int DeactivateLocal(Context var1, byte[] var2, byte[] var3, byte[] var4);

    public native String GetLastLMErrorString();

    public native String GetLMRevision();

    private native int CreateDecoder(int var1, int var2);

    private native void DestroyDecoder();

    private native int ReleaseDecoder();

    private native int DecodeImage(byte[] var1, int var2, int var3);

    public native void SetMaxResults(int var1);

    public native int SetProperty(int var1, int var2);

    public native int SetStringProperty(int var1, byte[] var2);

    public native int GetProperty(int var1);

    public native String GetStringProperty(int var1);

    public native String ReportDecoderVersion(int var1);

    public native String GetResultString(int var1);

    public native byte[] GetResultByteData(int var1);

    public native String GetSymbologyString(int var1);

    public native double GetDecodeTime(int var1);

    public native int GetLength(int var1);

    public native long[] GetSymIds(int var1);

    public native int[] GetBounds(int var1);

    public boolean isActivated() {
        return this.actRes == 1;
    }

    public static DecodeManager getInstance(Context context) {
        if(instance == null) {
            instance = new DecodeManager(context);
        }

        return instance;
    }

    public static void destroyInstance() {
        try {
            if(instance != null) {
                instance.DestroyDecoder();
                instance.ReleaseDecoder();
                instance = null;
            }
        } catch (Exception var1) {
            HSMLog.e(var1);
        }

    }

    private DecodeManager(Context context) {
        try {
            this.context = context;
            System.loadLibrary("jniload");
            this.LoadDecoder(context);
        } catch (Exception var3) {
            HSMLog.e(var3);
        }

    }

    private void setDecoderDefaults() {
        this.SetMaxResults(10);
        this.SetProperty(436219908, 0);
        this.SetProperty(436342786, 6);
        byte[] ocrUserTemplate = new byte[]{1, 3, 8, 15, 2, 20, 0};
        this.SetStringProperty(-1711091710, ocrUserTemplate);
        this.SetProperty(453169155, 1);
        this.SetProperty(436211729, 0);
        this.SetProperty(436211730, 0);
    }

    public void enableDecoding(boolean enable) {
        this.decoderEnabled = enable;
    }

    public boolean isDecodingEnabled() {
        return this.decoderEnabled;
    }

    public HSMDecodeResult[] decode(byte[] baImage, int previewWidth, int previewHeight) {
        HSMLog.trace();
        HSMDecodeResult[] decodeResults = new HSMDecodeResult[0];

        try {
            if(!this.isDecodingEnabled()) {
                return decodeResults;
            } else {
                int resultCount = this.DecodeImage(baImage, previewWidth, previewHeight);
                decodeResults = new HSMDecodeResult[resultCount];

                for(int i = 0; i < resultCount; ++i) {
                    byte[] data = this.GetResultByteData(i);
                    int length = this.GetLength(i);
                    String sym = this.GetSymbologyString(i);
                    Double decTime = Double.valueOf(this.GetDecodeTime(i));
                    com.honeywell.barcode.BarcodeBounds bounds = new com.honeywell.barcode.BarcodeBounds(this.GetBounds(i), previewWidth, previewHeight);
                    long[] ids = this.GetSymIds(i);
                    decodeResults[i] = new HSMDecodeResult(data, length, sym, decTime.doubleValue(), bounds, ids);
                }

                return decodeResults;
            }
        } catch (Exception var13) {
            HSMLog.e(var13);
            return decodeResults;
        }
    }

    public int activate(String rightsId) {
        HSMLog.trace();

        try {
            byte[] baRightsId = this.nullTerminate(rightsId.getBytes());
            this.actRes = this.RemoteActivation(this.context, baRightsId, DeviceInfo.getDeviceInfo(this.context));
//            Size previewSize = CameraManager.getInstance(this.context).getCurrentPreviewSize();
            if(this.CreateDecoder(1920,1080) != 1) {
                return 0;
            } else {
                this.setDecoderDefaults();
                return this.actRes;
            }
        } catch (Exception var4) {
            HSMLog.e(var4);
            return 0;
        }
    }

    public int activateWithLocalFile(String rightsId, byte[] activationFile) {
        HSMLog.trace();

        try {
            byte[] baRightsId = this.nullTerminate(rightsId.getBytes());
            this.actRes = this.LocalFileActivation(this.context, baRightsId, activationFile);
            Size previewSize = CameraManager.getInstance(this.context).getCurrentPreviewSize();
            return this.CreateDecoder(previewSize.width, previewSize.height) != 1?0:this.actRes;
        } catch (Exception var5) {
            HSMLog.e(var5);
            return 0;
        }
    }

    public int activateWithLocalServer(String rightsId, String localServerURL, byte[] identityClient) {
        HSMLog.trace();

        try {
            byte[] baRightsId = this.nullTerminate(rightsId.getBytes());
            byte[] baServerURL = this.nullTerminate(localServerURL.getBytes());
            this.actRes = this.LocalServerActivation(this.context, baRightsId, baServerURL, identityClient, DeviceInfo.getDeviceInfo(this.context));
            Size previewSize = CameraManager.getInstance(this.context).getCurrentPreviewSize();
            return this.CreateDecoder(previewSize.width, previewSize.height) != 1?0:this.actRes;
        } catch (Exception var7) {
            HSMLog.e(var7);
            return 0;
        }
    }

    public int deactivate(String rightsId) {
        HSMLog.trace();
        this.actRes = 0;

        try {
            byte[] baRightsId = this.nullTerminate(rightsId.getBytes());
            int res = this.Deactivate(this.context, baRightsId);
            if(res == 1) {
                Size previewSize = CameraManager.getInstance(this.context).getCurrentPreviewSize();
                if(this.CreateDecoder(previewSize.width, previewSize.height) == 0) {
                    return res;
                }
            }

            return res;
        } catch (Exception var5) {
            HSMLog.e(var5);
            return 0;
        }
    }

    public int deactivateLocal(String rightsId, String localServerURL, byte[] identityClient) {
        HSMLog.trace();
        this.actRes = 0;

        try {
            byte[] baRightsId = this.nullTerminate(rightsId.getBytes());
            byte[] baServerURL = this.nullTerminate(localServerURL.getBytes());
            int res = this.DeactivateLocal(this.context, baRightsId, baServerURL, identityClient);
            if(res == 1) {
                Size previewSize = CameraManager.getInstance(this.context).getCurrentPreviewSize();
                if(this.CreateDecoder(previewSize.width, previewSize.height) == 0) {
                    return res;
                }
            }

            return res;
        } catch (Exception var8) {
            HSMLog.e(var8);
            return 0;
        }
    }

    private byte[] nullTerminate(byte[] src) {
        byte[] dest = new byte[src.length + 1];
        System.arraycopy(src, 0, dest, 0, src.length);
        dest[src.length] = 0;
        return dest;
    }
}
